package com.xinzhi.service.impl;

import com.xinzhi.mapper.BankaccountMapper;
import com.xinzhi.mapper.PayeeMapper;
import com.xinzhi.pojo.Bankaccount;
import com.xinzhi.pojo.Payee;
import com.xinzhi.service.PayeeService;
import com.xinzhi.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class PayeeServiceImpl implements PayeeService {
    @Autowired
    private PayeeMapper payeeMapper;
    @Autowired
    private BankaccountMapper bankaccountMapper;
    @Override
    public Result getAll(Integer user_id) {
        if (user_id ==null)
            return Result.error("用户id不能为空");
        List<Payee> allPayee = payeeMapper.getAllPayee(user_id);
        return allPayee==null?Result.error("查询失败"):Result.ok("查询成功"+allPayee);
    }

    /**
     * 根据银行卡号和id新增一条收款人信息
     * @param
     * @return
     */
    @Override
    public Result insertPayee(String card_number,Integer id) {
        Boolean aBoolean = bankaccountMapper.selectPayee(card_number, id);
        if (aBoolean==null)
            return Result.ok("收款人已存在");
        Bankaccount bankaccount = bankaccountMapper.selectMoney(card_number);
        Payee payee = new Payee();
        payee.setId(id);
        payee.setUserId(bankaccount.getId());
        payee.setUsername(bankaccount.getUsername());
        payee.setCardNumber(card_number);
        payee.setCardType(bankaccount.getCardType());
        payee.setOpeningBank(bankaccount.getOpeningBank());
        int i = payeeMapper.insertPayee(payee);
        return i>0 ?Result.ok("添加成功") :Result.error("添加失败");
    }

    /**
     * 用户转账时判断用户有没有绑定收款人
     * @param user_id
     * @return
     */
    @Override
    public Result selectPayee(Integer user_id) {
        List<Payee> payees = payeeMapper.selectPayee(user_id);

        if (payees!=null){
          return Result.ok(payees);
        }
          return Result.error("查询失败");
//        int i = payeeMapper.insertPayee(payee);
//
//        return i>0?Result.ok("添加成功"):Result.error("添加失败");
    }
}
